#include <stdio.h>
#include "davinci-intc.h"
#include "davinci-psc.h"
#include "addressmap.h"
#include "davinci.h"


void log(char *,...);

#define N_PSC 1
static union 
{
	struct davinci_psc dat;
	unsigned int w[1];
	unsigned char b[1];
	unsigned short s[1];
}psc [N_PSC];



void davinci_psc_handler(
	  void *lpContext,
	  int op, 
	  unsigned int dwAddress,   // start dwAddress on 32bit bus
	  char *pszBuff,           // optional 
	  unsigned int nBytes )    // in or out buffer
{
	int n_channel=0;//*(char *)(lpContext);
	int offset;

	 if(n_channel<N_PSC
		 &&(offset=dwAddress&0xfff)<DAVINCI_PSC_ADDR_SIZE)
	 {
	
		 if(op==ADDR_RANGE_WRITE)
		 log("PSC %d: w %x %x\n",n_channel,dwAddress,offset,*(__u32*)pszBuff);
		 switch(op)
		 {
		 case ADDR_RANGE_READ: 
			 psc[0].b[offset]++;
			 psc[0].b[offset]&=3;

			
			 for(int i=0;i<nBytes;i++)
			 {
				 pszBuff[i]=psc[n_channel].b[i+offset];

			 }
			 break;
		 case ADDR_RANGE_WRITE: 
			 for(int i=0;i<nBytes;i++)
			 {
				 psc[n_channel].b[i+offset]=pszBuff[i];

			 }
			 break;
		 default:
			 return;

		 }
		 if(op==1)
		 log("PSC %d: r %x %x\n",n_channel,dwAddress,offset,*(__u32*)pszBuff);
	 }
	 //log("psc%d:%x %x %x\n",n_channel,op,dwAddress,nBytes);
}



void davinci_psc_do_clock()
{
	for(int i=0;i<N_PSC;i++)
	{

	}


}